import java.io.*;
import java.net.*;
import java.util.*;
import local.util.*;

public class CleanDupTickInTwoGroup {   
 
    private static boolean debug     = false;
    private static Vector  tickHoldV = new Vector (10);

    private static String input  = new String ();
    private static Vector vInput = new Vector (10);
    private static Vector vOut   = new Vector (10);
    private static String url    = new String ();
    private static String gId    = new String ();

    public static void main(String[] args) {

	if (args.length<1) {
	    System.out.println("[Group_ID]");
	}

	String g_id1 = args[0];
	CleanDupTickInTwoGroup c = new CleanDupTickInTwoGroup();
	//c.findDupGroupId (args[0]);
	Hashtable g_p_hashtable = new Hashtable();

	GroupProfileLoader gpfloader = new GroupProfileLoader();
	gpfloader.setFileName("./Data/Group/IndustryGroup.txt");
	gpfloader.loadProfile();
	g_p_hashtable = gpfloader.getHashTable();
	//gpfloader.printHashTable(g_p_hashtable);

	String tick_file_name1    = null;
	String tick1 = null;

	TickFileLoader floader = new TickFileLoader ();
	tick_file_name1 = "./Data/Naz/"+g_id1+".txt";

	Hashtable g_tick_hashtable1 = new Hashtable();
	
	floader.setFileName (tick_file_name1);
	floader.loadFile();
	g_tick_hashtable1 = floader.getHashTable();

	String g_name1 = (String)g_p_hashtable.get(g_id1);

	c.insertIntoDataBase(g_tick_hashtable1, g_id1, g_name1, "15");
    }

    private void findDupGroupId (String g_id1) {

	Hashtable g_p_hashtable = new Hashtable();

	GroupProfileLoader gpfloader = new GroupProfileLoader();
	gpfloader.setFileName("./Data/Group/IndustryGroup.txt");
	gpfloader.loadProfile();
	g_p_hashtable = gpfloader.getHashTable();
	//gpfloader.printHashTable(g_p_hashtable);

	String tick_file_name1    = null;
	String tick_file_name2    = null;
	String tick1 = null;

	TickFileLoader floader = new TickFileLoader ();
	tick_file_name1 = "./Data/Group/"+g_id1+".txt";

	Hashtable g_tick_hashtable1 = new Hashtable();
	
	floader.setFileName (tick_file_name1);
	floader.loadFile();
	g_tick_hashtable1 = floader.getHashTable();

	String g_id   = null;
	String g_name = null;
	int t1 = 0;
	int t2 = 0;
	Enumeration enum1 = g_tick_hashtable1.keys();  
	String temp = (String)g_p_hashtable.get(g_id1);
	Vector group_name_h_v = new Vector();
	while (enum1.hasMoreElements()) {
	    t1++;
	    tick1 = (String)enum1.nextElement();
	    //System.out.println("TICK="+tick1);

	    Enumeration enum2 =   g_p_hashtable.keys();

	    while (enum2.hasMoreElements()) {

		g_id   = (String)enum2.nextElement();
		//System.out.println("g_id="+g_id);
		g_name = (String)g_p_hashtable.get(g_id);
		//System.out.println("g_name="+g_name);
		if (g_id.equals(g_id1)) {
		    continue;
		}

		Hashtable g_tick_hashtable2 = new Hashtable();		
		tick_file_name2 = "./Data/Group/"+g_id+".txt";
		floader.setFileName (tick_file_name2);
		floader.loadFile();
		g_tick_hashtable2 = floader.getHashTable();
	
		if (g_tick_hashtable2.contains(tick1)) {
		    if (!group_name_h_v.contains(g_name)) {
			System.out.println("Found "+ tick1 + " In "+ g_id + ".txt"+ "Group Name="+g_name);
			group_name_h_v.addElement(g_name);
		    }
		}

	    }
	}
    }

    private void insertIntoDataBase (Hashtable h_table, String g_id, String g_name, String ex) {
	String key = null;
	String tick = null;
	String query_head = "INSERT INTO group_record VALUES (";
	String query_string = null;
	StringBuffer query_bf = new StringBuffer();
	DataBaseInsert myInsert = new DataBaseInsert();	

	Enumeration enum = h_table.keys();
	while (enum.hasMoreElements()) {
	    key = (String)enum.nextElement();
	    tick = (String)h_table.get(key);
	    query_string = query_head+"'"+tick+"',"+"'"+g_id+"',"+"'"+g_name+"',"+"'"+ex+"'"+");";

	    System.out.println(query_string);
	    myInsert.setQueryString(query_string);
	    
	    if(myInsert.insertData() != 1) {
		myInsert.setQueryString(query_string);
	    }
	    
	}
    }

    private void printDupTickAndGroupName( Vector input_v) {

	Hashtable tick_hash1 = new Hashtable ();
	Vector selected_hash_v = new Vector ();
	String key1=null;
	String tick1=null;
	Hashtable tick_hash2 = new Hashtable ();
	String key2=null;
	String tick2=null;
	int counter=0;
	int total=0;
	String temp_name1=null;
	String temp_name2=null;

	Enumeration enumv1 = input_v.elements();

	while ( enumv1.hasMoreElements() ) {   
	    counter =0;
	    tick_hash1 = (Hashtable)enumv1.nextElement();
	    selected_hash_v.addElement(tick_hash1);
	    
	    Enumeration enumh1 = tick_hash1.keys();

	    // MyPrint.printLine("=",80);
	    while (enumh1.hasMoreElements()) {
		counter++;
		key1  = (String)enumh1.nextElement();
		tick1 = (String)tick_hash1.get(key1);
		
		if (tick1.startsWith("Sector Name")) {
		    temp_name1= key1+"\t"+tick1;
		}
	    }
 
	    Enumeration enumv2 = input_v.elements();
	    while ( enumv2.hasMoreElements() ) {   
		tick_hash2 = (Hashtable)enumv2.nextElement();
		if (selected_hash_v.contains(tick_hash2)) {
		    continue;
		}

		selected_hash_v.addElement(tick_hash2);

		Enumeration enumh2 = tick_hash2.keys();
		while (enumh2.hasMoreElements()) {
		    key2  = (String)enumh2.nextElement();
		    tick2 = (String)tick_hash2.get(key2);
		
		    if (tick2.startsWith("Sector Name")) {
			temp_name2 = key2+"\t"+tick2;
		    }

		    if (tick2.equals(tick1)&& tick1 != null && tick2 != null) {
			System.out.println(tick1+"\t"+temp_name1);
			System.out.println(tick2+"\t"+temp_name2);
		    }
		}
	    }
	}
    }
  
    private void printTickListToFile( Vector inputV) {
	MyFileWriter myWriter = new MyFileWriter();
	myWriter.setFileName("./Data/Group/"+gId+".txt");

	StringBuffer stBuf = new StringBuffer();
	stBuf.append("Test file writeing!\n");

	myWriter.useBufferedOutputStream(stBuf.toString());
    }
}






